package com.sanhe.rfmuscle.activity.work;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.sanhe.rfmuscle.App;
import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ActivityVrefBinding;
import com.sanhe.rfmuscle.event.AMaxEvent;
import com.sanhe.rfmuscle.event.AMinEvent;
import com.sanhe.rfmuscle.event.BMaxEvent;
import com.sanhe.rfmuscle.event.BMinEvent;
import com.sanhe.rfmuscle.event.Event;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class VrefActivity extends BaseWorkActivity {
    Typeface medium = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
    ActivityVrefBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityVrefBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.title1.setTypeface(medium);
        binding.title2.setTypeface(medium);
        binding.title3.setTypeface(medium);
        binding.title4.setTypeface(medium);
        binding.title5.setTypeface(medium);
        binding.ok.setTypeface(medium);

        binding.back.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x20));
        binding.ok.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x44));

        binding.aMaxDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x70));
        binding.aMaxUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x71));

        binding.bMaxDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x74));
        binding.bMaxUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x75));

        binding.aMinDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x72));
        binding.aMinUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x73));

        binding.bMinDown.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x76));
        binding.bMinUp.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x77));

        binding.ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg_press);
                }else if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg);
                }
                return false;
            }
        });

        updateAMin();
        updateBMin();
        updateAMax();
        updateBMax();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AMaxEvent event) {
        updateAMax();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(AMinEvent event) {
        updateAMin();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BMaxEvent event) {
        updateBMax();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BMinEvent event) {
        updateBMin();
    }

    private void updateAMax() {
        binding.aMax.setText(Event.aMaxEvent.getValue() / 100f + "");
    }

    private void updateAMin() {
        binding.aMin.setText(Event.aMinEvent.getValue() / 100f + "");
    }

    private void updateBMax() {
        binding.bMax.setText(Event.bMaxEvent.getValue() / 100f + "");
    }

    private void updateBMin() {
        binding.bMin.setText(Event.bMinEvent.getValue() / 100f + "");
    }
}
